@extends('admin.layouts.app')

@section('title', '编辑用户')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
    <h3>编辑用户</h3>
    <div>
        <a href="{{ route('admin.users.show', $user) }}" class="btn btn-info">
            <i class="fas fa-eye me-2"></i>查看详情
        </a>
        <a href="{{ route('admin.users.index') }}" class="btn btn-secondary">
            <i class="fas fa-arrow-left me-2"></i>返回列表
        </a>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <form action="{{ route('admin.users.update', $user) }}" method="POST">
            @csrf
            @method('PUT')
            
            <div class="mb-3">
                <label for="name" class="form-label">姓名</label>
                <input type="text" class="form-control @error('name') is-invalid @enderror" 
                       id="name" name="name" value="{{ old('name', $user->name) }}" required>
                @error('name')
                    <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
            
            <div class="mb-3">
                <label for="email" class="form-label">邮箱</label>
                <input type="email" class="form-control @error('email') is-invalid @enderror" 
                       id="email" name="email" value="{{ old('email', $user->email) }}" required>
                @error('email')
                    <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
            
            <div class="mb-3">
                <label for="password" class="form-label">密码 <small class="text-muted">(留空则不修改)</small></label>
                <input type="password" class="form-control @error('password') is-invalid @enderror" 
                       id="password" name="password">
                @error('password')
                    <div class="invalid-feedback">{{ $message }}</div>
                @enderror
            </div>
            
            <div class="mb-3">
                <label for="password_confirmation" class="form-label">确认密码</label>
                <input type="password" class="form-control" 
                       id="password_confirmation" name="password_confirmation">
            </div>
            
            <div class="mb-3">
                <label class="form-label">角色</label>
                <div class="row">
                    @foreach($roles as $role)
                    <div class="col-md-4">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" 
                                   id="role_{{ $role->id }}" name="roles[]" value="{{ $role->name }}"
                                   {{ in_array($role->name, old('roles', $user->roles->pluck('name')->toArray())) ? 'checked' : '' }}>
                            <label class="form-check-label" for="role_{{ $role->id }}">
                                {{ $role->name }}
                            </label>
                        </div>
                    </div>
                    @endforeach
                </div>
                @error('roles')
                    <div class="text-danger">{{ $message }}</div>
                @enderror
            </div>
            
            <div class="mb-3">
                <label class="form-label">组织关联</label>
                <div class="card mb-3">
                    <div class="card-header bg-light">
                        <h6 class="mb-0">管理的组织</h6>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            @foreach($organizations as $org)
                            <div class="col-md-4 mb-2">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" 
                                           id="admin_org_{{ $org->id }}" name="admin_organizations[]" value="{{ $org->id }}"
                                           {{ in_array($org->id, old('admin_organizations', $user->organizations->where('pivot.is_admin', true)->pluck('id')->toArray())) ? 'checked' : '' }}>
                                    <label class="form-check-label" for="admin_org_{{ $org->id }}">
                                        {{ $org->name }}
                                        <small class="text-muted">({{ $org->code }})</small>
                                    </label>
                                </div>
                            </div>
                            @endforeach
                        </div>
                        @error('admin_organizations')
                            <div class="text-danger">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
                
                <div class="card">
                    <div class="card-header bg-light">
                        <h6 class="mb-0">所属组织</h6>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            @foreach($organizations as $org)
                            <div class="col-md-4 mb-2">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" 
                                           id="org_{{ $org->id }}" name="organizations[]" value="{{ $org->id }}"
                                           {{ in_array($org->id, old('organizations', $user->organizations->where('pivot.is_admin', false)->pluck('id')->toArray())) ? 'checked' : '' }}>
                                    <label class="form-check-label" for="org_{{ $org->id }}">
                                        {{ $org->name }}
                                        <small class="text-muted">({{ $org->code }})</small>
                                    </label>
                                </div>
                            </div>
                            @endforeach
                        </div>
                        @error('organizations')
                            <div class="text-danger">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>
            
            <div class="d-flex justify-content-end">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save me-2"></i>更新用户
                </button>
            </div>
        </form>
    </div>
</div>
@endsection